<template>
  <div>
    <div class="loading" v-loading="pageLoading" element-loading-background="rgba(0, 0, 0, 0.3)"></div>
    <div class="wrap" v-show="!pageLoading">
      <div class="page2" id="content">
        <div class="item">
          <div class="info">*</div>
          <input type="number" v-model="telValue" placeholder="请输入您的电话号码" />
        </div>
        <div class="item">
          <div class="info">*</div>
          <input type="text" v-model="cdkeyValue" placeholder="请输入加油兑换码" />
        </div>
        <el-button class="btn" type="text" @click="addOilInit()" :loading="loadingBool">立即加油</el-button>
      </div>
      <a class="anchor" href="#content"></a>
    </div>
  </div>
</template>


<script>
import { Dialog } from "vant";
export default {
  data() {
    return {
      wechatUrl: "https://www.hailuo123.com/api/",
      // wechatUrl: "https://hellohailuo.com/api/",
      apiUrl: "https://www.chidouche.com/api/",
      // apiUrl: "https://eatbeancar.goho.co/",
      loadingBool: false,
      cdkeyValue: "", //兑换码
      telValue: "", //电话号码
      requestBool: true, //点击锁
      pageLoading: true //页面锁
    };
  },
  created() {
    // 适配
    this.adaptive();
    // 查看是否有缓存的电话号码并验证是否输入过code
    this.gainCacheTel();
  },
  methods: {
    /**
     * 获取缓存的手机号并验证该手机号的用户是否输入兑换码
     */
    async gainCacheTel() {
      let tel = localStorage.getItem("tel");
      console.log(tel);

      if (!tel) {
        this.pageLoading = false;
        setTimeout(() => {
          let anchor = document.getElementsByClassName("anchor")[0];
          anchor.click();
        }, 200);
        return;
      }
      let status = await this.verifyCodeBool(tel);
      console.log(status);

      if (+status === 1) {
        window.location.href = "https://st.czb365.com/v3_prod/?pft=92653325";
      } else if (+status === 0) {
        this.pageLoading = false;
        setTimeout(() => {
          let anchor = document.getElementsByClassName("anchor")[0];
          anchor.click();
        }, 200);
      }
    },
    /**
     * 适配
     */
    adaptive: function() {
      /*
       * doc   document
       * win   window
       * designWidth  设计稿的尺寸   避免还要用过dpr计算正确的根节点rem值
       */
      (function(doc, win, designWidth) {
        var html = doc.documentElement;
        var refreshRem = function() {
          var clientWidth = html.clientWidth;
          var vw = 100 * (clientWidth / designWidth);
          html.style.fontSize = (vw / clientWidth) * 100 + "vw";
        };

        // dom元素加载完毕就只该事件
        addEventListener("DOMContentLoaded", refreshRem, false);
      })(document, window, 750);
    },

    /**
     *加油
     */
    async addOilInit() {
      if (!this.requestBool) {
        return;
      }
      // 开启loading动画
      this.loadingBool = true;
      // 关闭锁
      this.requestBool = false;
      if (!this.cdkeyValue || !this.telValue) {
        this.requestBool = true;
        this.loadingBool = false;
        Dialog.alert({
          message: "请您输入手机号和兑换码。"
        });
        return;
      }
      if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.telValue)) {
        this.requestBool = true;
        this.loadingBool = false;
        Dialog.alert({
          message: "请输入正确的手机号。"
        });
        return false;
      }
      //判断该手机号的用户是否输入过code
      let status = await this.verifyCodeBool(this.telValue);
      console.log("!!!!", status);

      // status  0未使用   1已使用
      if (+status === 0) {
        // 判断用户输入的code是兑换码还是批次码  批次码返回兑换码
        let codeValue = await this.verifyCode_1();
        // 验证兑换码是否正确
        this.verifyCode_2(codeValue);
      } else if (+status === 1) {
        this.loadingBool = false;
        this.requestBool = true;
        Dialog.alert({
          message: "您已输入过兑换码，可直接加油!。"
        });
        this.addOil();
      }
    },

    /**
     * 验证兑换码是否是第一次输入
     */
    verifyCodeBool(tel) {
      return new Promise((resolve, reject) => {
        this.$axios({
          method: "post",
          url: this.apiUrl + "eatbeancar-life-points/refuelRecord/checkCode",
          data: {
            source: 1,
            mobile: tel
          }
        }).then(res => {
          if (res.data.code === 0) {
            resolve(res.data.data.type);
          } else {
            reject(res.data.msg);
          }
        });
      });
    },
    /**
     *  验证兑换码是否正确或者是否重复使用
     */
    verifyCode_2(codeValue) {
      this.$axios({
        method: "post",
        url: this.apiUrl + "eatbeancar-life-points/refuelRecord/checkCode_0",
        data: {
          code: codeValue,
          mobile: this.telValue,
          source: 1
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.loadingBool = false;
          this.requestBool = true;
          this.addOil();
        } else {
          this.loadingBool = false;
          this.requestBool = true;
          Dialog.alert({
            message: res.data.msg
          });
        }
      });
    },
    /**
     *验证用户输入的兑换码是批次号还是兑换码
     */
    verifyCode_1() {
      return new Promise((resolve, reject) => {
        this.$axios({
          method: "post",
          url: this.wechatUrl + "activity/open/dataTurn/get/",
          data: {
            num: this.cdkeyValue
          }
        }).then(res => {
          if (res.data.code == 0 && res.data.data) {
            //通过批次号换兑换码
            resolve(res.data.data.code);
          } else if (res.data.code == 0 && res.data.data == null) {
            //直接通过兑换码
            resolve(this.cdkeyValue);
          } else {
            reject(res.data.msg);
          }
        });
      });
    },
    /**
     * 跳转到车主邦加油
     */
    addOil() {
      Dialog.alert({
        message: "点击确认跳转加油。"
      }).then(() => {
        localStorage.setItem("tel", this.telValue);
        window.location.href = "https://st.czb365.com/v3_prod/?pft=92653325";
      });
    }
  }
};
</script>


<style scoped>
.wrap {
  background-size: cover;
  width: 100%;
  height: 25.05rem;
  background-image: url("https://china185.com/file/image/20191128/6aa87790f12f4e558a05bbdece6a0025.png");
}

.page2 {
  position: absolute;
  left: calc(50% - 3.43rem);
  top: 4.2rem;
  padding-top: 4rem;
  width: 6.86rem;
  height: 7.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.wrap .page2 .item {
  width: 100%;
  display: flex;
  height: 0.9rem;
}

.wrap .page2 .info {
  width: 0.85rem;
  height: 100%;
  color: #fa6d39;
  text-align: center;
  line-height: 0.9rem;
  font-size: 0.3rem;
}

.wrap input {
  width: 5.5rem;
  margin-right: 0.5rem;
  height: 0.9rem;
  border-radius: 0.1rem;
  font-size: 0.3rem;
  text-align: center;
  color: #868686;
  outline: none;
  border: none;
  background-color: rgb(242, 242, 242);
}

.wrap .item:nth-of-type(1) {
  margin-bottom: 0.3rem;
}

.wrap .item:nth-of-type(2) {
  margin-bottom: 0.5rem;
}

.wrap .btn {
  width: 3.21rem;
  height: 0.9rem;
  background-color: rgb(255, 208, 98);
  color: #333;
  font-weight: bold;
  border-radius: 0.45rem;
  font-size: 0.28rem;
  display: flex;
  justify-content: center;
  align-items: center;
  line-height: 0;
}

.wrap >>> .el-button--primary {
  border-color: none;
}

.loading {
  width: 100vw;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
}

.anchor {
  display: none;
}
</style>